package com.lingyun.mqttserver.controller;

import com.lingyun.mqttserver.dao.pojo.DeviceGroup;
import com.lingyun.mqttserver.service.ManageDataService;
import com.lingyun.mqttserver.vo.AddDeviceToGroupVo;
import com.lingyun.mqttserver.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("device-group")
public class DeviceGroupController {

    @Autowired
    private ManageDataService manageDataService;


    /**
     * 获取分组列表
     * @param projectId 项目ID（可选）
     * @return 分组列表
     */
    @GetMapping("groupList")
    public Result getGroupList(@RequestParam(required = false) Integer projectId,
                               @RequestParam(required = false) String groupName) {
            return manageDataService.getGroupList(projectId, groupName);
    }

    @PostMapping("groupAdd")
    public Result groupAdd(@RequestBody DeviceGroup deviceGroup){
        return manageDataService.groupAdd(deviceGroup);
    }

    @PostMapping("groupUpdate")
    public Result groupUpdate(@RequestBody DeviceGroup deviceGroup){
        return manageDataService.groupUpdate(deviceGroup);
    }

    @DeleteMapping("groupDelete")
    public Result groupDelete(@RequestParam Integer id){
        return manageDataService.groupDelete(id);
    }

    /**
     * 设备分组关联接口
     * @param groupId
     * @return
     */
    @GetMapping("groupDeviceList")
    public Result groupDeviceList(@RequestParam Integer groupId){
        return manageDataService.groupDeviceList(groupId);
    }

    /**
     * 添加设备到分组
     * @param addDeviceToGroupVo
     * @return
     */
    @PostMapping("addDeviceToGroup")
    public Result addDeviceToGroup(@RequestBody AddDeviceToGroupVo addDeviceToGroupVo){
        return manageDataService.addDeviceToGroup(addDeviceToGroupVo);
    }

    /**
     * 从分组移除设备
     * @param groupId
     * @param deviceId
     * @return
     */
    @DeleteMapping("removeDeviceFromGroup")
    public Result removeDeviceFromGroup(@RequestParam Integer groupId, @RequestParam Integer deviceId){
        return manageDataService.removeDeviceFromGroup(groupId, deviceId);
    }

    /**
     * 获取可添加设备列表
     * @param groupId
     * @param projectId
     * @return
     */
    @GetMapping("availableDevices")
    public Result availableDevices(@RequestParam Integer groupId, @RequestParam Integer projectId){
        return manageDataService.availableDevices(groupId, projectId);
    }
}
